#include <bits/stdc++.h>

using namespace std;

typedef struct Node {
    int val;
    Node *left;
    Node *right;
    Node() : val(0), left(nullptr), right(nullptr) {}
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
    Node(int x, Node *left, Node *right) : val(x), left(left), right(right) {}
}*PNode;

class Solution {
private:
    int post_idx;
    unordered_map<int, int> ump;
    PNode _root;
    vector<int> inorder;
    vector<int> postorder;

    PNode _helper(int idxl, int idxr);
    void _helperPrint(PNode node);
public:
    Solution(vector<int> &_inorder, vector<int> &_postorder) :inorder(_inorder), postorder(_postorder) {
        int i = 0;
        for (auto value : inorder) {
            ump[value] = (i++);
        }
        post_idx = postorder.size() - 1;
        _root = _helper(0, (int)inorder.size() - 1);
    }

    void PreOrderprintTree() {
        _helperPrint(_root);
    }

};

int main() {
    vector<int> inorder, postorder;

    string readed;
    int num;

    getline(cin, readed);
    stringstream readedstream1(readed);
    while (readedstream1 >> num) {
        inorder.push_back(num);
    }

    getline(cin, readed);
    stringstream readedstream2(readed);
    while (readedstream2 >> num) {
        postorder.push_back(num);
    }

    Solution solution(inorder, postorder);
    solution.PreOrderprintTree();
    //system("pause");
    return 0;
}

PNode Solution::_helper(int idxl, int idxr) {
    if (idxl > idxr || post_idx < 0) return nullptr;
    int nodeval = postorder[post_idx];
    PNode root = new Node(nodeval);
    int in_idx = ump[nodeval];
    post_idx--;
    root->right = _helper(in_idx + 1, idxr);
    root->left = _helper(idxl, in_idx - 1);
    return root;
}

void Solution::_helperPrint(PNode root) {
    if (root == nullptr) { return; }
    cout << root->val << " ";
    _helperPrint(root->left);
    _helperPrint(root->right);
}


